package com.kitty.game.gm;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.base.service.BagService;
import com.kitty.game.equip.service.EquipService;
import com.kitty.game.equip.service.FabaoService;
import com.kitty.game.party.service.PartyService;
import com.kitty.game.pay.model.PayNotifyInfo;
import com.kitty.game.pay.service.OrderService;
import com.kitty.game.pet.model.Pet;
import com.kitty.game.role.model.Role;
import com.kitty.game.role.service.PayService;
import com.kitty.game.role.service.RoleService;
import com.kitty.common.model.Reason;
import org.nutz.lang.random.R;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class GmCommandHandler100 {

    public void fabao18ji(Role role, String content) {
        int xiangxin = 0;
        if (content.contains("(金)")) {
            xiangxin = 1;
        } else if (content.contains("(木)")) {
            xiangxin = 2;
        } else if (content.contains("(水)")) {
            xiangxin = 3;
        } else if (content.contains("(火)")) {
            xiangxin = 4;
        } else if (content.contains("(土)")) {
            xiangxin = 5;
        }
        String equipname = content.substring(3, content.indexOf("("));
        SpringUtils.getBean(FabaoService.class).getArtifact18(role, equipname, xiangxin);
    }

    public void chongzhi(Role role, String content) {
        String[] strings = content.split("\\|");
        PayNotifyInfo payNotifyInfo = new PayNotifyInfo();
        payNotifyInfo.setMoney(Integer.parseInt(strings[0]));
        payNotifyInfo.setTradeNo("MY-"+R.UU32());
        payNotifyInfo.setAccount(role.getSid());
        payNotifyInfo.setRoleUid(role.getUid());
        payNotifyInfo.setGold(Integer.parseInt(strings[1]));
        payNotifyInfo.setCreateTime(new Date());
        SpringUtils.getBean(OrderService.class).handleOrder(payNotifyInfo);
    }


    public void money(Role role, int money) {
        SpringUtils.getRoleService().addMoney(role, money, Reason.GM_ADD_MONEY);
    }


    public void exp(Role role, int money) {
        SpringUtils.getRoleService().addExp(role, money, 1, role.getPetBox().getFightPetId());
    }

    public void pot(Role role, int pot) {
        SpringUtils.getRoleService().addPot(role, pot);
    }

    public void matiral(Role role, int matiral) {
        Pet pet = role.getPetBox().getPetByPetId(role.getPetBox().getFightPetId());
        SpringUtils.getRoleService().addPetMatiral(role, pet, matiral);
    }

    public void gold(Role role, int money) {
        SpringUtils.getRoleService().addGold(role, money, Reason.GM_ADD_GOLD);
        SpringUtils.getRoleService().updateRoleGoldAndSiver(role);
    }
    public void level(Role role, int level) {
        SpringUtils.getBean(GmService.class).level(role,level);
    }

    public void shuxin(Role role, int money) {
        role.setStr((short) money);
        role.setCon((short) money);
        role.setWiz((short) money);
        role.setDex((short) money);
        SpringUtils.getBean(RoleService.class).sendExtraAttribute(role);
    }

//    public void pet(Role role, String petName) {
//        PetObject petObject = PetDataPool.getPetObject(petName);
//        if (petObject.getPetType() ==  PetType.ghost?
//                !SpringUtils.getPetService().isEquippedGhostFull(role)
//                : !SpringUtils.getPetService().isEquippedFull(role)) {
//            if (petObject != null) {
//                Pet pet = SpringUtils.getBean(PetService.class).addPet(petObject, role, false);
//                SpringUtils.getBean(PetService.class).loadPet(role, pet);
//            }
//        } else {
//            MessagePusher.pushMessage(role, new RespMsg("当前宠物包裹已满，请整理后再来吧。"));
//        }
//    }

    public void jifen(Role role, int score) {
        SpringUtils.getRoleService().addRechargeScore(role, score);
    }

    public void choujiang(Role role, int score) {
        SpringUtils.getBean(PayService.class).choujiang(role, score);
    }

    public void yd(Role role, int score) {
        SpringUtils.getRoleService().yinde(role, score);
    }


    public void equip(Role role, String name) {
        SpringUtils.getBean(EquipService.class).getEquip(role, name);
    }

    public void banggong(Role role, int value) {
        SpringUtils.getBean(PartyService.class).addContrib(role, value);
    }


    /**
     * {"混元金斗", "番天印","阴阳镜","定海珠","金蛟剪","九龙神火罩","卸甲金葫"};
     */
    public void fabao(Role role, String name) {
        SpringUtils.getBean(EquipService.class).getArtifact(name, role);
    }

    public void fabao18(Role role, String name) {
        SpringUtils.getBean(FabaoService.class).getArtifact18(role, name, 1);
    }

    public void fabao24(Role role, String name) {
        SpringUtils.getBean(FabaoService.class).getArtifact24(role, name, 1);
    }



    public void daohang(Role role, int daohang) {
        SpringUtils.getBean(RoleService.class).addTao(role, daohang);
    }


    public void zhuangbei(Role role, int level) {
        EquipService equipService = SpringUtils.getBean(EquipService.class);
        if (level > 0 && level <= 120) {
            BagService bagService = SpringUtils.getBean(BagService.class);
            short pos = bagService.getPos(role, false);
            if (pos >= 41) {
                equipService.getNotIdentifyEquip(level, role, pos);
            }
        }
    }



    public void shoushi(Role role, String name) {
        EquipService equipService = SpringUtils.getBean(EquipService.class);
        BagService bagService = SpringUtils.getBean(BagService.class);
        short pos = bagService.getPos(role, false);
        if (pos >= 41) {
            equipService.getJewelry(role, name, false, 1, true);
        }
    }

//    public void shoushiX5(Role role, String name) {
//        EquipService equipService = SpringUtils.getBean(EquipService.class);
//        BagService bagService = SpringUtils.getBean(BagService.class);
//        short pos = bagService.getPos(role, false);
//        if (pos >= 41) {
//            equipService.getJewelry(role, name, true, 1, true);
//        }
//    }

//    public void quitMY(Role role, String aa) {
//        FightService fightService = SpringUtils.getBean(FightService.class);
//        Fight fight = fightService.getFightByRoleId(role.getRoleId());
//        if (fight == null) {
//            return;
//        }
//        fightService.quitFight(fight);
//    }


























    // gm管理员的命令


//    public void startShidao(Role role, String content) {
//        String[] strings = content.split("##");
//        SpringUtils.getBean(ShiDaoHandler.class).setStartH(Integer.parseInt(strings[0]));
//        SpringUtils.getBean(ShiDaoHandler.class).setStartM(Integer.parseInt(strings[1]));
//        SpringUtils.getBean(ShiDaoHandler.class).prepareOnceShiDao();
//    }


//    public void name(Role role, String content) {
//        content = content.trim();
//        String[] strings = content.split("##");
//        Role temp = SpringUtils.getBean(Dao.class).fetch(Role.class, Cnd.where("roleId", "=", Integer.parseInt(strings[0])));
//        if (temp == null) {
//            return;
//        }
//        String name = strings[1];
//        PlayerService playerService = SpringUtils.getPlayerService();
//        List<PlayerProfile> list = playerService.getPlayersBy(temp.getSid());
//        for (PlayerProfile playerProfile : list) {
//            if (temp.getRoleId() == playerProfile.getRoleId()) {
//                playerProfile.setName(name);
//                break;
//            }
//        }
//        String oldName = temp.getName();
//        temp = playerService.getPlayerBy(temp.getUid());
//        temp.setName(name);
//        temp.save();
//        SpringUtils.getPlayerService().updateRoleName(oldName, role);
//    }


//    public void online(Role role, String name) {
//        MessagePusher.pushMessage(role, new RespNotifyMiscEx("在线人数为#R" + DataCache.ONLINE_ROLES.size() + "#n!!"));
//    }


//    public void jifenAll(Role role, int score) {
//        Map<Long, IoSession> map = SpringUtils.getBean(GameServer.class).getAcceptor().getManagedSessions();
//        for (Map.Entry<Long, IoSession> entry : map.entrySet()) {
//            IoSession session = entry.getValue();
//            Role tempRole = SessionUtils.getRoleBySession(session);
//            if (tempRole == null) {
//                continue;
//            }
//            SpringUtils.getBean(PayService.class).jifen(role, score);
//        }
//    }

//    public void yinyuanbaoAll(Role role, int score) {
//        RoleService roleService = SpringUtils.getRoleService();
//        Map<Long, IoSession> map = SpringUtils.getBean(GameServer.class).getAcceptor().getManagedSessions();
//        for (Map.Entry<Long, IoSession> entry : map.entrySet()) {
//            IoSession session = entry.getValue();
//            Role tempRole = SessionUtils.getRoleBySession(session);
//            if (tempRole == null) {
//                continue;
//            }
//            roleService.addSiver(tempRole,score,Reason.AGENT_GM);
//        }
//    }

//    public void jinyuanbaoAll(Role role, int score) {
//        RoleService roleService = SpringUtils.getRoleService();
//        Map<Long, IoSession> map = SpringUtils.getBean(GameServer.class).getAcceptor().getManagedSessions();
//        for (Map.Entry<Long, IoSession> entry : map.entrySet()) {
//            IoSession session = entry.getValue();
//            Role tempRole = SessionUtils.getRoleBySession(session);
//            if (tempRole == null) {
//                continue;
//            }
//            roleService.addGold(tempRole, score, Reason.AGENT_GM);
//        }
//    }

//    public void choujiangAll(Role role, int value) {
//        Map<Long, IoSession> map = SpringUtils.getBean(GameServer.class).getAcceptor().getManagedSessions();
//        for (Map.Entry<Long, IoSession> entry : map.entrySet()) {
//            IoSession session = entry.getValue();
//            Role tempRole = SessionUtils.getRoleBySession(session);
//            if (tempRole == null) {
//                continue;
//            }
//            SpringUtils.getBean(PayService.class).choujiang(role, value);
//        }
//    }


//    public void gonggao(Role role, String content) {
//        Account account = SpringUtils.getBean(AccountService.class).getAccount(role.getSid());
//        if (account.getPrivilege() < PrivilegeConst.GM) {
//            return;
//        }
//        SpringUtils.getBean(ChatService.class).sendAdnotice(content.split("==")[1]);
//    }


//    public void bangpai(Role role, String name) {
//        String[] names = name.split("##");
//        Dao dao = SpringUtils.getBean(Dao.class);
//        Party party = dao.fetch(Party.class, Cnd.where("partyName", "=", names[0]));
//        if (party == null) {
//            MessagePusher.pushMessage(role, new RespNotifyMiscEx("帮派不存在"));
//            return;
//        }
//        PartyService partyService = SpringUtils.getPartyService();
//        party = partyService.getParty(party.getId());
//        party.setPartyName(names[1]);
//        party.setNotice(names[1]);
//        party.save();
//        MessagePusher.pushMessage(role, new RespNotifyMiscEx("修改成功！！！"));
//    }
}
